package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.utils.Mask;

public class CadastroFragment extends GenericoFragment {

    private TextView textViewNomeTelaCadastro;
    private TextView textViewOrientacaoSenha;
    private EditText editTextNomeCadastro;
    private EditText editTextEmailCadastro;
    private EditText editTextCpfCadastro;
    private Button buttonCadastrarCadastro;
    private ProgressBar progressBarCadastro;

    private AcoesCadastrarFragment activity;
    private CadastroTask task;

    private String nome;
    private String email;
    private String cpf;

    View.OnClickListener clickButtonCadastrarCadastro = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            nome = editTextNomeCadastro.getText().toString();
            email = editTextEmailCadastro.getText().toString();
            cpf = editTextCpfCadastro.getText().toString();

            if (nome.trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_nome);
            } else if(email.trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_email);
            } else if (Mask.unmask(cpf).trim().equals(P.STRING_EMPITY)) {
                activity.onSpeakAndMakeText(R.string.obrigatoriedade_cpf);
            } else {
                if (activity.isConnected()) {
                    task = new CadastroTask();
                    task.execute();
                } else {
                    activity.onSpeakAndMakeText(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesCadastrarFragment) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cadastro, container, false);

        textViewNomeTelaCadastro = (TextView) view.findViewById(R.id.textViewNomeTelaLogin);
        textViewOrientacaoSenha = (TextView) view.findViewById(R.id.textViewOrientacaoSenha);
        editTextNomeCadastro = (EditText) view.findViewById(R.id.editTextNomeCadastro);
        editTextEmailCadastro = (EditText) view.findViewById(R.id.editTextEmailCadastro);
        editTextCpfCadastro = (EditText) view.findViewById(R.id.editTextCpfCadastro);
        buttonCadastrarCadastro = (Button) view.findViewById(R.id.buttonCadastrarCadastro);
        progressBarCadastro = (ProgressBar) view.findViewById(R.id.progressBarCadastro);

        editTextCpfCadastro.addTextChangedListener(Mask.insert("###.###.###-##", editTextCpfCadastro));

        buttonCadastrarCadastro.setOnClickListener(clickButtonCadastrarCadastro);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            hideComponents();
            progressBarCadastro.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void showComponents() {
        textViewNomeTelaCadastro.setVisibility(View.VISIBLE);
        textViewOrientacaoSenha.setVisibility(View.VISIBLE);
        editTextNomeCadastro.setVisibility(View.VISIBLE);
        editTextEmailCadastro.setVisibility(View.VISIBLE);
        editTextCpfCadastro.setVisibility(View.VISIBLE);
        buttonCadastrarCadastro.setVisibility(View.VISIBLE);
        progressBarCadastro.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideComponents() {
        textViewNomeTelaCadastro.setVisibility(View.GONE);
        textViewOrientacaoSenha.setVisibility(View.GONE);
        editTextNomeCadastro.setVisibility(View.GONE);
        editTextEmailCadastro.setVisibility(View.GONE);
        editTextCpfCadastro.setVisibility(View.GONE);
        buttonCadastrarCadastro.setVisibility(View.GONE);
        progressBarCadastro.setVisibility(View.GONE);
    }

    private class CadastroTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            hideComponents();
            progressBarCadastro.setVisibility(View.VISIBLE);
            activity.onSpeak(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.onCadastrarAluno(nome, email, cpf);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            showComponents();
            progressBarCadastro.setVisibility(View.GONE);

            if (aBoolean) {
                activity.onMakeText(R.string.cadastro_realizado_com_sucesso);
            } else {
                activity.onSpeakAndMakeText(R.string.excecao_cadastro_aluno_malsucedido);
            }
        }
    }

    public interface AcoesCadastrarFragment extends AcoesGenericoFragment {
        Boolean onCadastrarAluno(String nome, String email, String cpf);
    }
}
